import { apiUrl } from "../../api.js";
import { StorageKeys } from "../../api.js";

const $api = require('../../utils/request.js');

const app = getApp()
Page({
  data: {
    StatusBar: app.globalData.StatusBar,
    CustomBar: app.globalData.CustomBar,
    Custom: app.globalData.Custom,
    TabCur: 0,
    MainCur: 0,
    VerticalNavTop: 0,
    locationList: [],
    lockerList: [],
    selectedLocationName: '',
    load: true
  }, 
  onShow: function (e) { 
    //这边可以指定 bar 选中样式
    if (typeof this.getTabBar === 'function' && this.getTabBar()) {
      this.getTabBar().setData({ selected: 3 }); // 0 是首页在 list 中的索引 
    } 
    this.setData({
      locationList: wx.getStorageSync(StorageKeys.LOCATION_LIST).list
    })
    this.getLockerList(this.data.locationList[0].id)
  },
  showDetail(e) {
    //app.globalData.lockerDetail = e.currentTarget.dataset.detail
    
    console.log(e)
    console.log(222)
    wx.navigateTo({
      url: "/pages/detail/detail",
      success: function (res) {
        console.log(e.currentTarget.dataset.id)
        console.log(3333)
        // 通过eventChannel向被打开页面传送数据
        res.eventChannel.emit('acceptLockerData', { data:e.currentTarget.dataset.id })
      }
    })
  },
  getLockerList(e) {
    var obj = {
      memberId__eq: wx.getStorageSync(StorageKeys.TOKEN),
      locationId__eq: e
    };
    $api.post(apiUrl + '/locker/list', obj)
      .then(res => {
        console.info("res", res)
        if (res.list) {
          const newLockerList = res.list.map(v => {
            if (v.itemImages) {
              // 提取第一个图片（如果只需要第一张）
              const firstImage = v.itemImages.split(',')[0].trim().replace(/[\[\]]/g, '');
              return { ...v, image: apiUrl + firstImage };
            }
            return v;
          });
          
          this.setData({ lockerList: newLockerList }); // 
        }
      })
      .catch(err => {
        //请求失败
        console.info(err)
        wx.showModal({
          title: '提示',
          content: JSON.stringify(err),//'这是一个模态弹窗',
          showCancel: false,
        })
      })
  },

  NavChange(e) {
    wx.navigateTo({
      url: e.currentTarget.dataset.cur
    })
  },
  onLoad() {
    wx.showLoading({
      title: '加载中...',
      mask: true
    });
    let list = [{}];
    for (let i = 0; i < 26; i++) {
      list[i] = {};
      list[i].name = String.fromCharCode(65 + i);
      list[i].id = i;
    }
    this.setData({
      list: list,
      listCur: list[0]
    })
  },
  onReady() {
    wx.hideLoading()
  },
  tabSelect(e) {
    this.getLockerList(e.currentTarget.dataset.id)
    this.setData({
      TabCur: e.currentTarget.dataset.id,
      MainCur: e.currentTarget.dataset.id,
      VerticalNavTop: 0 //(e.currentTarget.dataset.id - 1) * 50
    })
  },
  VerticalMain(e) {
    let that = this;
    let list = this.data.list;
    let tabHeight = 0;
    if (this.data.load) {
      for (let i = 0; i < list.length; i++) {
        let view = wx.createSelectorQuery().select("#main-" + list[i].id);
        view.fields({
          size: true
        }, data => {
          list[i].top = tabHeight;
          tabHeight = tabHeight + data.height;
          list[i].bottom = tabHeight;
        }).exec();
      }
      that.setData({
        load: false,
        list: list
      })
    }
    let scrollTop = e.detail.scrollTop + 20;
    for (let i = 0; i < list.length; i++) {
      if (scrollTop > list[i].top && scrollTop < list[i].bottom) {
        that.setData({
          VerticalNavTop: (list[i].id - 1) * 50,
          TabCur: list[i].id
        })
        return false
      }
    }
  }
})